package com.hbt.android.kotlin.http


import com.google.gson.Gson
import com.google.gson.reflect.TypeToken


fun main() {
    val jsonStr ="{\"uid\":\"00001\",\"userName\":\"Freeman\",\"password\":\"password\",\"telNumber\":\"13000000000\"}";
    val gson = Gson()
    val accountObj = gson.fromJson<Account>(jsonStr, Account::class.java)
    println("将json字符串转成对象：" + accountObj.toString())

    val account = Account()
    println("将对象转成json字符串：" + gson.toJson(account))

    val jsonList ="[{\"uid\":\"00001\",\"userName\":\"Freeman\",\"password\":\"password\",\"telNumber\":\"13000000000\"}]";

    val accountList: List<Account> = gson.fromJson(jsonList, object :TypeToken<List<Account>>(){}.type)
    println("将json字符串转成对象集合：" + accountList.toString())

    val list = ArrayList<Account>()
    list.add(account)
    println("将对象集合转成json字符串：" + gson.toJson(list))


}

class Account {
    var uid:String = "00001"
    var userName:String = "root"
    var password:String = "root"
    var telNumber:String = "18888888888"
    override fun toString(): String {
        return "Account(uid='$uid', userName='$userName', password='$password', telNumber='$telNumber')"
    }
}

